//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows;

namespace OpenLS.UI.Interop
{
    internal static class NativeMethods
    {
        internal const int MONITOR_DEFAULTTOPRIMARY = 1;
        internal const int MONITOR_DEFAULTTONEAREST = 2;
        internal const int SWP_NOOWNERZORDER = 512;
        internal const int SWP_NOZORDER = 4;

        internal static bool IsVista
        {
            get
            {
#if FORCE_XP
                return false;
#endif
                return (Environment.OSVersion.Version.Major >= 6);
            }
        }

        [DllImport("dwmapi.dll")]
        internal static extern int DwmDefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, out IntPtr result);

        [DllImport("dwmapi.dll")]
        public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMargins);



        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        internal static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);

        [DllImport("user32.dll")]
        internal static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfo lpmi);


        public static IntPtr CallDwmBase(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam)
        {
            IntPtr result;
            DwmDefWindowProc(hWnd, msg, wParam, lParam, out result);
            return result;
        }


        [DllImport("USER32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        [DllImport("USER32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, int lParam);






        [DllImport("USER32.dll")]
        public static extern int GetWindowLong(IntPtr hwnd, int flags);

        [DllImport("USER32.dll")]
        public static extern int SetWindowLong(IntPtr hwnd, int flags, int val);

        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out POINT lpPoint);


        [SecurityCritical, SuppressUnmanagedCodeSecurity]
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy,
                                               int flags);


        [DllImport("user32.dll")]
        private static extern bool AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);

        internal static RECT GetBorderRect(IntPtr handle)
        {
            int style = GetWindowLong(handle, WindowLong.GWL_STYLE);
            int exStyle = GetWindowLong(handle, WindowLong.GWL_EXSTYLE);
            var result = new RECT(0, 0, 100, 100);
            AdjustWindowRectEx(ref result, style, false, exStyle);
            result.Left = Math.Abs(result.Left);
            result.Top = Math.Abs(result.Top);
            result.Right -= 100;
            result.Bottom -= 100;

            return result;
        }

        internal static bool IsMaximized(IntPtr hwnd)
        {
            int style = GetWindowLong(hwnd, WindowLong.GWL_STYLE);
            return ((style & WindowsStyles.WS_MAXIMIZE) != 0);
        }

        [SecurityCritical]
        public static bool IsCompositionEnabled()
        {
           //\\ return false; //\\
#if FORCE_XP
            return false;
#endif
            if (!IsVista)
            {
                return false;
            }
            bool isEnabled = false;
            DwmIsCompositionEnabled(ref isEnabled);
            return isEnabled;
        }

        [DllImport("dwmapi.dll")]
        private static extern void DwmIsCompositionEnabled(ref bool isEnabled);

        [DllImport("shell32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
        internal static extern void SHCreateItemFromParsingName([MarshalAs(UnmanagedType.LPWStr)] string pszPath,
                                                                IntPtr pbc, ref Guid riid, out IntPtr ppv);

        #region Nested type: BITMAPINFO_SMALL


        #endregion

       /* #region Nested type: COMDLG_FILTERSPECUnsafe

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        internal struct COMDLG_FILTERSPECUnsafe
        {
            [MarshalAs(UnmanagedType.LPWStr)]
            public string pszName;

            [MarshalAs(UnmanagedType.LPWStr)]
            public string pszSpec; 
            //internal IntPtr pszName;
            //internal IntPtr pszSpec;
        }

        #endregion
        */

       

        #region Nested type: FDAP

        internal enum FDAP
        {
        }

        #endregion

        #region Nested type: FOS

        [Flags]
        internal enum FOS : uint
        {
        }

        #endregion

        #region Nested type: Margins

        [StructLayout(LayoutKind.Sequential)]
        public struct Margins
        {
            public int Left;
            public int Right;
            public int Top;
            public int Bottom;
        }

        #endregion

        #region Nested type: MinMaxInfo

        [StructLayout(LayoutKind.Sequential)]
        public struct MinMaxInfo
        {
            private POINT ptReserved;
            public POINT ptMaxSize;
            public POINT ptMaxPosition;
            private POINT ptMinTrackSize;
            private POINT ptMaxTrackSize;

            public static MinMaxInfo GetFrom(IntPtr lParam)
            {
                return (MinMaxInfo) Marshal.PtrToStructure(lParam, typeof (MinMaxInfo));
            }
        }

        #endregion

        #region Nested type: MONITORINFO

        [StructLayout(LayoutKind.Sequential)]
        public struct MonitorInfo
        {
            public void Initialize()
            {
                cbSize = (uint) Marshal.SizeOf(this);
            }

            public uint cbSize;
            public RECT rcMonitor;
            public RECT rcWork;
            public uint dwFlags;
            //[MarshalAs(UnmanagedType.ByValArray, SizeConst = CCHDEVICENAME)]
            //public string[] szDevice;
        }

        #endregion

        #region Nested type: NcCalcSizeParams

        [StructLayout(LayoutKind.Sequential)]
        public struct NcCalcSizeParams
        {
            public RECT rgrc0;
            internal RECT rgrc1;
            public RECT rgrc2;
            public IntPtr lppos;

            public static NcCalcSizeParams GetFrom(IntPtr lParam)
            {
                return (NcCalcSizeParams) Marshal.PtrToStructure(lParam, typeof (NcCalcSizeParams));
            }
        }

        #endregion

        #region Nested type: POINT

        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;

            public POINT(Point pt)
            {
                X = (int) pt.X;
                Y = (int) pt.Y;
            }

            public POINT(int dw)
            {
                X = (short) LOWORD(dw);
                Y = (short) HIWORD(dw);
            }

            private static int LOWORD(int n)
            {
                return (n & 65535);
            }


            private static int HIWORD(int n)
            {
                return ((n >> 16) & 65535);
            }

            public int ToInt32()
            {
                return Y << 16 | X;
            }

            public Point ToPoint()
            {
                return new Point(X, Y);
            }

            public override string ToString()
            {
                return string.Format("X={0} , Y={1}", X, Y);
            }
        }

        #endregion

        #region Nested type: PROPERTYKEY

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        internal struct PROPERTYKEY
        {
            internal Guid fmtid;
            internal uint pid;
        }

        #endregion

        #region Nested type: SIATTRIBFLAGS

        internal enum SIATTRIBFLAGS
        {
        }

        #endregion

        #region Nested type: SIGDN

        internal enum SIGDN : uint
        {
            SIGDN_DESKTOPABSOLUTEPARSING = 0x80028000, // SHGDN_FORPARSING
        }

        #endregion

    }
}